/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerMainFrame.java
 *
 * Created on 2011-4-11, 14:13:11
 */
package cs1555prj;

import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author sddys
 */
public class CustomerMainFrame extends javax.swing.JFrame {

    /** Creates new form CustomerMainFrame */
    public CustomerMainFrame(MyDataBase m) {
        initComponents();
        mainTab.setMnemonicAt(0, KeyEvent.VK_1);
        mainTab.setMnemonicAt(1, KeyEvent.VK_2);
        mainTab.setMnemonicAt(2, KeyEvent.VK_3);
        mainTab.setMnemonicAt(3, KeyEvent.VK_4);
        db = m;

        DefaultTreeModel ctm = new DefaultTreeModel(db.getCatelogs());

        CatalogTree.setModel(ctm);
        CatalogTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        CatalogTree3.setModel(ctm);

        //tab1
        DefaultTableModel model = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {    //make cell uneditable
                return false;
            }
        };
        model.addColumn("Aucdtion id");
        model.addColumn("Name");
        model.addColumn("Description");
        model.addColumn("Seller");
        model.addColumn("Start Date");
        model.addColumn("Number of Days");
        model.addColumn("Highest Bid");
        model.addColumn("Bidder");

        table1.setModel(model);
        table1.getColumnModel().getColumn(0).setPreferredWidth(5);//auction id
        table1.getColumnModel().getColumn(1).setPreferredWidth(60);//name
        table1.getColumnModel().getColumn(2).setPreferredWidth(100);//description
        table1.getColumnModel().getColumn(3).setPreferredWidth(5);//seller
        table1.getColumnModel().getColumn(4).setPreferredWidth(60);//start date
        table1.getColumnModel().getColumn(5).setPreferredWidth(10);//number of days
        table1.getColumnModel().getColumn(6).setPreferredWidth(30);//bidding amount
        table1.getColumnModel().getColumn(7).setPreferredWidth(10);//bidder
        table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        edDesc.setLineWrap(true);

        //tab2
        DefaultTableModel model2 = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {    //make cell uneditable
                return false;
            }
        };
        model2.addColumn("Aucdtion id");
        model2.addColumn("Name");
        model2.addColumn("Description");
        model2.addColumn("Seller");
        model2.addColumn("Start Date");
        model2.addColumn("Min Price");
        model2.addColumn("Number of Days");
        model2.addColumn("Status");

        table2.setModel(model2);
        table2.getColumnModel().getColumn(0).setPreferredWidth(5);//auction id
        table2.getColumnModel().getColumn(1).setPreferredWidth(60);//name
        table2.getColumnModel().getColumn(2).setPreferredWidth(100);//description
        table2.getColumnModel().getColumn(3).setPreferredWidth(5);//seller
        table2.getColumnModel().getColumn(4).setPreferredWidth(60);//start date
        table2.getColumnModel().getColumn(5).setPreferredWidth(10);//min price
        table2.getColumnModel().getColumn(6).setPreferredWidth(10);//number of days
        table2.getColumnModel().getColumn(7).setPreferredWidth(10);//status
        table2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);




    }

    /**clear all rows in jtable*/
    private void clearAllRows(JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        int numrows = model.getRowCount();
        for (int i = numrows - 1; i >= 0; i--) {
            model.removeRow(i);
        }
    }

    /**get all child categories and store them in an arraylist*/
    private void getAllChildCategories(ArrayList<String> list, DefaultMutableTreeNode node) {
        if (!node.isRoot() && !node.isLeaf()) {
            list.add((String) node.getUserObject());
        }

        if (node.isLeaf()) {
            list.add((String) node.getUserObject());
        } else {
            for (int i = 0; i < node.getChildCount(); i++) {
                getAllChildCategories(list, (DefaultMutableTreeNode) node.getChildAt(i));
            }
        }
    }

    /**set user name*/
    public void setUser(String user) {
        login = user;
        String userName = "";
        userName = db.getNameByLogin(user, true);
        //System.out.println("user; " + userName);
        if (userName.equals("")) {
            System.out.println("login " + user + " don't have a user name!? What the hack is happending here!");
        }
        setTitle("Welcome " + userName + "\n");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainTab = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        combSort = new javax.swing.JComboBox();
        btnT1Showall = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        CatalogTree = new javax.swing.JTree();
        jScrollPane1 = new javax.swing.JScrollPane();
        table1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        edKeyword1 = new javax.swing.JTextField();
        edKeyword2 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        btnT2Search = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        table2 = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        CatalogTree3 = new javax.swing.JTree();
        jLabel7 = new javax.swing.JLabel();
        edName = new javax.swing.JTextField();
        edDays = new javax.swing.JTextField();
        btnAuction = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        edPrice = new javax.swing.JTextField();
        jScrollPane5 = new javax.swing.JScrollPane();
        edDesc = new javax.swing.JTextArea();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                onWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        mainTab.setName("mainTab"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cs1555prj.Cs1555PrjApp.class).getContext().getResourceMap(CustomerMainFrame.class);
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        combSort.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Auction Id", "Bid Amount", "Name of Product", " " }));
        combSort.setName("combSort"); // NOI18N
        combSort.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                combSortMouseClicked(evt);
            }
        });

        btnT1Showall.setText(resourceMap.getString("btnT1Showall.text")); // NOI18N
        btnT1Showall.setName("btnT1Showall"); // NOI18N
        btnT1Showall.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnT1ShowallActionPerformed(evt);
            }
        });

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        CatalogTree.setName("CatalogTree"); // NOI18N
        CatalogTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CatalogTreeMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(CatalogTree);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        table1.setName("table1"); // NOI18N
        table1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                table1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(table1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnT1Showall, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(combSort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 819, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addGap(11, 11, 11)
                .addComponent(combSort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnT1Showall, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(72, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
        );

        mainTab.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        edKeyword1.setText(resourceMap.getString("edKeyword1.text")); // NOI18N
        edKeyword1.setName("edKeyword1"); // NOI18N

        edKeyword2.setText(resourceMap.getString("edKeyword2.text")); // NOI18N
        edKeyword2.setName("edKeyword2"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        btnT2Search.setText(resourceMap.getString("btnT2Search.text")); // NOI18N
        btnT2Search.setName("btnT2Search"); // NOI18N
        btnT2Search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnT2SearchActionPerformed(evt);
            }
        });

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        table2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        table2.setName("table2"); // NOI18N
        jScrollPane2.setViewportView(table2);

        jLabel9.setFont(resourceMap.getFont("jLabel9.font")); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel9.setName("jLabel9"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel4)
                        .addComponent(jLabel5)
                        .addComponent(edKeyword2)
                        .addComponent(edKeyword1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnT2Search, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 833, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(edKeyword1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(edKeyword2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnT2Search, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(260, Short.MAX_VALUE))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
        );

        mainTab.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        jPanel3.setName("jPanel3"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        CatalogTree3.setName("CatalogTree3"); // NOI18N
        jScrollPane4.setViewportView(CatalogTree3);

        jLabel7.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        edName.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        edName.setText(resourceMap.getString("edName.text")); // NOI18N
        edName.setName("edName"); // NOI18N

        edDays.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        edDays.setText(resourceMap.getString("edDays.text")); // NOI18N
        edDays.setName("edDays"); // NOI18N

        btnAuction.setFont(resourceMap.getFont("btnAuction.font")); // NOI18N
        btnAuction.setForeground(resourceMap.getColor("btnAuction.foreground")); // NOI18N
        btnAuction.setText(resourceMap.getString("btnAuction.text")); // NOI18N
        btnAuction.setName("btnAuction"); // NOI18N
        btnAuction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAuctionActionPerformed(evt);
            }
        });

        jLabel8.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        edPrice.setText(resourceMap.getString("edPrice.text")); // NOI18N
        edPrice.setName("edPrice"); // NOI18N

        jScrollPane5.setName("jScrollPane5"); // NOI18N

        edDesc.setColumns(20);
        edDesc.setRows(5);
        edDesc.setName("edDesc"); // NOI18N
        jScrollPane5.setViewportView(edDesc);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(110, 110, 110)
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(edName, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(edPrice)
                                        .addComponent(edDays, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)))))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(238, 238, 238)
                        .addComponent(btnAuction, javax.swing.GroupLayout.PREFERRED_SIZE, 443, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(edName, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(edDays, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(edPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(23, 23, 23)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))))
                .addGap(33, 33, 33)
                .addComponent(btnAuction, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(78, 78, 78))
        );

        mainTab.addTab(resourceMap.getString("jPanel3.TabConstraints.tabTitle"), jPanel3); // NOI18N

        jPanel4.setName("jPanel4"); // NOI18N

        jScrollPane6.setName("jScrollPane6"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane6.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 802, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(184, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 518, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        mainTab.addTab(resourceMap.getString("jPanel4.TabConstraints.tabTitle"), jPanel4); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainTab, javax.swing.GroupLayout.DEFAULT_SIZE, 1014, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainTab)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void onWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_onWindowClosed
    }//GEN-LAST:event_onWindowClosed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        Cs1555PrjApp.getApplication().getMainFrame().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_formWindowClosing

    private void btnT1ShowallActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnT1ShowallActionPerformed
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) CatalogTree.getLastSelectedPathComponent();
        if (node == null) {
            System.out.println("No node being selected");
            return;
        } else {    //user select some categories
            ArrayList<String> cat = new ArrayList<String>();
            getAllChildCategories(cat, node);
            String sortby = "";
            switch (combSort.getSelectedIndex()) {
                case 0:
                    sortby = "auction_id";
                    break;
                case 1:
                    sortby = "max1 desc";
                    break;
                case 2:
                    sortby = "product1";
                    break;
            }
            //do query to get data
            ResultSet rs = db.getProductsByCategories(cat.toArray(), sortby);
            if (rs == null) {
                System.out.println("get production by categories fail!\n");
            } else {//result return successfully
                clearAllRows(table1);
                try {
                    ArrayList<String> newrow = new ArrayList<String>();

                    while (rs.next()) {
                        newrow.clear();
                        /* System.out.println(rs.getInt("auction_id") + " " + rs.getString("product1") + " " + rs.getString("desc1")
                        + " " + rs.getString("seller1") + " " +rs.getString("date1") + " "+rs.getString("days1") + " "
                        + rs.getInt("max1") + " " + rs.getString("bidder1"));*/
                        newrow.add(Integer.toString(rs.getInt("auction_id")));
                        newrow.add(rs.getString("product1"));
                        newrow.add(rs.getString("desc1"));
                        newrow.add(rs.getString("seller1"));
                        newrow.add(rs.getString("date1"));
                        newrow.add(rs.getString("days1"));
                        newrow.add(rs.getString("max1"));
                        newrow.add(rs.getString("bidder1"));
                        ((DefaultTableModel) table1.getModel()).addRow(newrow.toArray());
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(CustomerMainFrame.class.getName()).log(Level.SEVERE, null, ex);
                    System.out.println("Result handling fail\n!");
                }
            }
        }

        //update data in table1

    }//GEN-LAST:event_btnT1ShowallActionPerformed

    private void table1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_table1MouseClicked
        if (evt.getClickCount() == 2) { //double click
            JTable target = (JTable) evt.getSource();
            int row = target.getSelectedRow();
            String rowSeller = (String) target.getValueAt(row, 3);
            int selectAuctionId = Integer.parseInt(target.getValueAt(row, 0).toString());
            if (!rowSeller.equals(login)) {//it's not the seller
                BiddingDialog bdlg = new BiddingDialog(this, true, selectAuctionId, db, login);
                bdlg.setLocationRelativeTo(this);
                bdlg.setVisible(true);
            } else {//it's the seller
                SellingDialog sdlg = new SellingDialog(this, true, selectAuctionId, db);
                sdlg.setLocationRelativeTo(this);
                sdlg.setVisible(true);
            }
            btnT1ShowallActionPerformed(null);
        }
    }//GEN-LAST:event_table1MouseClicked

    private void CatalogTreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CatalogTreeMouseClicked
        btnT1ShowallActionPerformed(null);
    }//GEN-LAST:event_CatalogTreeMouseClicked

    private void combSortMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_combSortMouseClicked
        //btnT1ShowallActionPerformed(null);
    }//GEN-LAST:event_combSortMouseClicked

    private void btnT2SearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnT2SearchActionPerformed
        String key1 = edKeyword1.getText(), key2 = edKeyword2.getText();
        if (key1.equals("") && key2.equals("")) {    //if both keywords is null
            JOptionPane.showMessageDialog(null, "There must be at least one key words!");
            return;
        }

        ResultSet rs = db.getAllProductsByKeywords(key1, key2);
        if (rs == null) {
            System.out.println("get production by categories fail!\n");
        } else {//result return successfully
            clearAllRows(table2);
            try {
                ArrayList<String> newrow = new ArrayList<String>();
                while (rs.next()) {
                    newrow.clear();
                    newrow.add(rs.getString("auction_id"));
                    newrow.add(rs.getString("name"));
                    newrow.add(rs.getString("description"));
                    newrow.add(rs.getString("seller"));
                    newrow.add(rs.getString("start_date"));
                    newrow.add(rs.getString("min_price"));
                    newrow.add(rs.getString("status"));
                    ((DefaultTableModel) table2.getModel()).addRow(newrow.toArray());
                }
            } catch (SQLException ex) {
                Logger.getLogger(CustomerMainFrame.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Result handling fail\n!");
            }
        }
    }//GEN-LAST:event_btnT2SearchActionPerformed

    private void btnAuctionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAuctionActionPerformed
        if(edName.getText().equals("") || edPrice.getText().equals("") || edDays.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Name or price or days can't be empty");
            return;
        }
        TreePath[] path = CatalogTree3.getSelectionPaths();
        ArrayList<String> list = new ArrayList<String>();   //contains all categories
        if(path == null) {
            JOptionPane.showMessageDialog(null, "You Must select at least one leaf category!");
            return;
        }
        for (TreePath tmp : path) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tmp.getLastPathComponent();
            if (node.isLeaf()) {
                list.add(node.getUserObject().toString());
            } else {
                JOptionPane.showMessageDialog(null, node.getUserObject() + " is not a leaf node\n");
                return;
            }
        }
        System.out.println(list);
        db.putProductOnAuction(edName.getText(), edDesc.getText(), login,
                Integer.parseInt(edPrice.getText()), Integer.parseInt(edDays.getText()), list);
        
        
    }//GEN-LAST:event_btnAuctionActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree CatalogTree;
    private javax.swing.JTree CatalogTree3;
    private javax.swing.JButton btnAuction;
    private javax.swing.JButton btnT1Showall;
    private javax.swing.JButton btnT2Search;
    private javax.swing.JComboBox combSort;
    private javax.swing.JTextField edDays;
    private javax.swing.JTextArea edDesc;
    private javax.swing.JTextField edKeyword1;
    private javax.swing.JTextField edKeyword2;
    private javax.swing.JTextField edName;
    private javax.swing.JTextField edPrice;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTable jTable1;
    private javax.swing.JTabbedPane mainTab;
    private javax.swing.JTable table1;
    private javax.swing.JTable table2;
    // End of variables declaration//GEN-END:variables
    private MyDataBase db;
    private String login = "";  //login name
}
